//
//  RJLoginChoseView.m
//  AirQuality
//
//  Created by Po on 15/5/4.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJLoginChoseView.h"
@interface RJLoginChoseView () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (retain, nonatomic) UIPickerView * picker;

@property (assign, nonatomic) CGRect pickerFrame;
@property (retain, nonatomic) NSMutableArray * citys;

- (void)initNet;
@end

@implementation RJLoginChoseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:ScreenBounds];
    if (self) {
        _pickerFrame = frame;
        [self initNet];
        [self initAllButtons];
    }
    return self;
}

- (void)initNet
{
    [RJNetWork myNetRequestWithAddress:@"http://newair.scemc.cn:5100/smartadmin/home/getCityStationStatus?stationtypeid=1" data:@{} completeBlock:^(id responseObject) {
        _citys = [NSMutableArray arrayWithArray:responseObject[@"DATA"]];
        [_citys insertObject:@{@"CITYNAME":@"省站人员"} atIndex:0];
        [self addSubview:[self getPickerViewWithRect:_pickerFrame]];
    }failBlack:^(id response) {
        
    }];
}

- (UIPickerView *)getPickerViewWithRect:(CGRect)rect
{
    if (!_picker) {
        _picker = [[UIPickerView alloc] initWithFrame:rect];
        [_picker setDataSource:self];
        [_picker setDelegate:self];
        [_picker setBackgroundColor:[UIColor whiteColor]];
    }
    return _picker;
}

- (void)initAllButtons
{
    CGFloat y = ScreenHeight - 260;
    CGFloat width = ScreenWidth / 2;
    for (NSUInteger i = 0; i < 2; i ++) {
        UIButton * button = [self createButtonWithFrame:CGRectMake(ScreenWidth / 2 * i, y, width, 44) title:(i == 0?@"取消":@"确定") backImage:[UIImage imageNamed:(i == 0 ?@"reasonViewCancel":@"reasonViewComfirm")]];
        if (i == 0) {
            [button addTarget:self action:@selector(pressCancelButton) forControlEvents:UIControlEventTouchUpInside];
        } else {
            [button addTarget:self action:@selector(pressConfirmButton) forControlEvents:UIControlEventTouchUpInside];
        }
        [self addSubview:button];
    }
}

- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title backImage:(UIImage *)image
{
    UIButton * button = [[UIButton alloc] initWithFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    return button;
}

#pragma mark - event
- (void)pressConfirmButton
{
    if (_delegate) {
        NSUInteger rowNum = [_picker selectedRowInComponent:0];
        [_delegate LoginChoseView:self didEndSelectCityData:_citys[rowNum]];
    }
}

- (void)pressCancelButton
{
    [self freeSelf];
}

- (void)freeSelf
{
    [self removeFromSuperview];
}

#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _citys.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_citys[row] objectForKey:@"CITYNAME"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString * cityName = [_citys[row] objectForKey:@"CITYNAME"];
}
@end
